package com.ruoyi.project.person.dept.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.cms.CmsNotDeleteException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.person.dept.domain.PersonDept;
import com.ruoyi.project.person.dept.domain.PersonDeptVo;
import com.ruoyi.project.person.dept.service.IPersonDeptService;
import com.ruoyi.project.person.info.domain.PersonInfo;
import com.ruoyi.project.person.info.domain.PersonInfoVo;
import com.ruoyi.project.person.info.service.IPersonInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program:
 * @auther 草莓酸奶~
 * @date 2020-09-21-10:00
 * @description:
 */

@Controller
@RequestMapping("/person/dept")
public class MyDeptController extends BaseController {


    private String prefix = "person/dept";

    @Autowired
    private IPersonDeptService personDeptService;

    @RequiresPermissions("person:dept:view")
    @GetMapping()
    public String content() {
        return prefix + "/dept";
    }

    @RequiresPermissions("person:dept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PersonDept personDept){
        startPage();
        List<PersonDept> list=  personDeptService.selectPersonDeptList(personDept);
        return getDataTable(list);
    }


    @RequiresPermissions("person:dept:add")
    @GetMapping("/add")
    public String personDeptadd() {
        return prefix + "/add";
    }


    @RequiresPermissions("person:dept:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(PersonDept personDept) {


        return toAjax(personDeptService.insertPersonDept(personDept));
    }


    @RequiresPermissions("person:view:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {

        try {
            int result = personDeptService.removePersonDeptById(ids);
            return toAjax(result);
        } catch (CmsNotDeleteException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap modelMap) {

        PersonDept personDept = personDeptService.selectDeptById(deptId);
        modelMap.addAttribute("personDept", personDept);
        return prefix + "/edit";
    }
    /**
     * @param personDept
     * @return
     */
    @RequiresPermissions("person:view:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated PersonDept personDept) {

        return toAjax( personDeptService.updatePersonDept(personDept));
    }



}
